//
//  FWCommentController.m
//  BirdLoveSheep
//
//  Created by wwj on 16/3/29.
//  Copyright © 2016年 wwj. All rights reserved.
//

#import "FWCommentController.h"
#import "UIBarButtonItem+Extension.h"
#import "FWComHeaterView.h"
#import "FWEssenceCell.h"
#import "FWEssenceService.h"
#import "FWTopicFrameModel.h"
#import "FWTopicModel.h"
#import "FWComCell.h"
#import "FWComHeaderView.h"
#import "FWTopComment.h"
#import <MJRefresh.h>

@interface FWCommentController ()<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *buttomCons;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *hotArray;
@property (strong, nonatomic) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) NSInteger total;
@end

@implementation FWCommentController

static NSString *const ID = @"FWComCell";
static NSString *const HeadID = @"FWComHeaderView";

- (NSArray *)hotArray{
    if (_hotArray == nil) {
        _hotArray = [NSArray array];
    }
    return _hotArray;
}

- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([FWComCell class]) bundle:nil] forCellReuseIdentifier:ID];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([FWComHeaderView class]) bundle:nil] forHeaderFooterViewReuseIdentifier:HeadID];
    self.tableView.estimatedRowHeight = 44;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    [self addRefresh];
}

- (void)addRefresh{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getNewData)];
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreData)];
}


- (void)initView{
    self.view.backgroundColor = kBgColor;
    self.navigationItem.title = @"评论";
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem fw_itemWithIcon:@"comment_nav_item_share_icon" highIcon:@"comment_nav_item_share_icon_click" target:self action:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    FWComHeaterView *headerView = [[FWComHeaterView alloc] init];
    headerView.modelF = self.modelF;
    self.tableView.tableHeaderView = headerView;
}

- (void)keyboardWillChangeFrame:(NSNotification *)notification{
    CGRect keyBoardF = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    self.buttomCons.constant = kScreenH - keyBoardF.origin.y;
    CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    [UIView animateWithDuration:duration animations:^{
        [self.view layoutIfNeeded];
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.hotArray.count) {
        return 2;
    }
    if (self.dataArray.count){
        return 1;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    self.tableView.mj_footer.hidden = !self.dataArray.count;
    if (section == 0) {
        if (self.hotArray.count) {
            return self.hotArray.count;
        }
    }
    return self.dataArray.count;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    FWComHeaderView *headerView = (FWComHeaderView *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:HeadID];
    if (section == 0) {
        headerView.title = self.hotArray.count ? @"最热评论" : @"最新评论";
    }else {
        headerView.title = @"最新评论";
    }
    
    return headerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FWComCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    cell.model = [self getDataArrayWithIndexPath:indexPath][indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIMenuController *menuVC = [UIMenuController sharedMenuController];
    if (menuVC.isMenuVisible) {
        [menuVC setMenuVisible:NO animated:YES];
        return;
    }
    UIMenuItem *ding = [[UIMenuItem alloc] initWithTitle:@"顶" action:@selector(ding:)];
    UIMenuItem *replay = [[UIMenuItem alloc] initWithTitle:@"回复" action:@selector(replay:)];
    UIMenuItem *report = [[UIMenuItem alloc] initWithTitle:@"举报" action:@selector(report:)];
    menuVC.menuItems = @[ding, replay, report];
    
    FWComCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell becomeFirstResponder];
    [menuVC setTargetRect:CGRectMake(0, cell.height * 0.5, cell.width, cell.height) inView:cell];
    [menuVC setMenuVisible:YES animated:YES];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
    [[UIMenuController sharedMenuController] setMenuVisible:NO animated:YES];
}

- (void)ding:(UIMenuController *)menu{
    MyLog(@"%s", __func__);
}

- (void)replay:(UIMenuController *)menu{
    MyLog(@"%s", __func__);
}

- (void)report:(UIMenuController *)menu{
    MyLog(@"%s", __func__);
}

- (NSArray *)getDataArrayWithIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (self.hotArray.count) {
            return self.hotArray;
        }
    }
    return self.dataArray;
}



- (void)getNewData{
    FWComRquest *request = [[FWComRquest alloc] init];
    request.a = @"dataList";
    request.c = @"comment";
    request.data_id = self.modelF.topic.ID;
    request.hot = @"1";
    request.page = 1;
    [FWEssenceService getcommentDataWith:request success:^(FWComResponse *response) {
        if (self.dataArray.count >= self.total) {
           
            self.tableView.mj_footer.hidden = YES;
        }

        [self.tableView.mj_header endRefreshing];
        self.page = 1;
        [self.dataArray removeAllObjects];
        self.total = response.total;
        self.hotArray = response.hot;
        self.dataArray = [response.data mutableCopy];
        [self.tableView reloadData];
        
        
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void)getMoreData{
    FWComRquest *request = [[FWComRquest alloc] init];
    request.a = @"dataList";
    request.c = @"comment";
    request.data_id = self.modelF.topic.ID;
    request.page = ++self.page;
    [FWEssenceService getcommentDataWith:request success:^(FWComResponse *response) {
        if (self.dataArray.count >= self.total) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        [self.tableView.mj_footer endRefreshing];
        [self.dataArray addObjectsFromArray:response.data];
        [self.tableView reloadData];
        
        
    } failure:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
    }];

}


- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
